import 'package:bruno/bruno.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:interrogation/src/data/api.dart';

import '../../../../../app/routes/app_pages.dart';
import '../../../../../assets/const.dart';
import '../controllers/doctor_mine_page_controller.dart';

class DoctorMinePageView extends GetView<DoctorMinePageController> {
  const DoctorMinePageView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey[100],
      body: SafeArea(
        child: Column(
          children: [
            // 顶部专业信息区
            Padding(
              padding: EdgeInsets.all(16.w),
              child: Container(
                height: 140.h,
                decoration: BoxDecoration(
                  color: const Color(0xFFE3F2FD),
                  borderRadius: BorderRadius.circular(16.r),
                ),
                padding: EdgeInsets.all(16.w),
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    ClipRRect(
                      borderRadius: BorderRadius.circular(8.r),
                      child: CachedNetworkImage(
                        imageUrl: controller.avatarUrl,
                        imageBuilder: (context, image) => CircleAvatar(
                          foregroundImage: image,
                          radius: 18,
                        ),
                        errorWidget: (context, url, error) => Container(
                          decoration: BoxDecoration(
                            color: Colors.black.withOpacity(0.5),
                            borderRadius: BorderRadius.circular(18),
                          ),
                        ),
                        placeholder: (context, url) => Container(
                          decoration: BoxDecoration(
                            color: const Color(0xffF1F1F1),
                            borderRadius: BorderRadius.circular(18),
                          ),
                        ),
                      ),
                    ),
                    SizedBox(width: 16.w),
                    Expanded(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Row(
                            children: [
                              Text('张某某',
                                  style: TextStyle(
                                      fontSize: 18.sp,
                                      fontWeight: FontWeight.bold)),
                              SizedBox(width: 8.w),
                              Text('主任医师',
                                  style: TextStyle(
                                      fontSize: 16.sp, color: Colors.black87)),
                            ],
                          ),
                          SizedBox(height: 4.h),
                          Text('XX市人民医院',
                              style: TextStyle(
                                  fontSize: 14.sp, color: Colors.grey[600])),
                          const Spacer(),
                          Row(
                            children: [
                              SizedBox(
                                width: 80.w,
                                height: 32.h,
                                child: ElevatedButton(
                                  onPressed: () {},
                                  style: ElevatedButton.styleFrom(
                                    backgroundColor: const Color(0xFF2196F3),
                                    shape: RoundedRectangleBorder(
                                      borderRadius: BorderRadius.circular(8.r),
                                    ),
                                    padding: EdgeInsets.zero,
                                  ),
                                  child: Text('排班管理',
                                      style: TextStyle(
                                          fontSize: 14.sp,
                                          color: Colors.white)),
                                ),
                              ),
                              SizedBox(width: 12.w),
                              SizedBox(
                                width: 80.w,
                                height: 32.h,
                                child: ElevatedButton(
                                  onPressed: () {},
                                  style: ElevatedButton.styleFrom(
                                    backgroundColor: const Color(0xFF607D8B),
                                    shape: RoundedRectangleBorder(
                                      borderRadius: BorderRadius.circular(8.r),
                                    ),
                                    padding: EdgeInsets.zero,
                                  ),
                                  child: Text('绩效评估',
                                      style: TextStyle(
                                          fontSize: 14.sp,
                                          color: Colors.white)),
                                ),
                              ),
                            ],
                          ),
                          SizedBox(height: 6),
                          Text('执业证号：110XXXXXXXXXXX',
                              style: TextStyle(
                                  fontSize: 12.sp, color: Colors.grey[700])),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
            // 功能模块区
            Padding(
              padding: EdgeInsets.symmetric(horizontal: 16.w),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  _buildFuncCard(
                    icon: Icons.edit,
                    label: '资料编辑',
                    bgColor: const Color(0xFFF5F9FF),
                    iconColor: const Color(0xFF2196F3),
                    onTap: () {},
                  ),
                  _buildFuncCard(
                    icon: Icons.folder_open,
                    label: '病例',
                    bgColor: const Color(0xFFF5F5F5),
                    iconColor: const Color(0xFF607D8B),
                    onTap: () {},
                  ),
                  _buildFuncCard(
                    icon: Icons.chat_bubble_outline,
                    label: '记录',
                    bgColor: const Color(0xFFF5F9FF),
                    iconColor: const Color(0xFF2196F3),
                    onTap: () {},
                  ),
                  _buildFuncCard(
                    icon: Icons.settings,
                    label: '设置',
                    bgColor: const Color(0xFFF5F5F5),
                    iconColor: const Color(0xFF607D8B),
                    onTap: () {},
                  ),
                ],
              ),
            ),
            const Spacer(),
            // 底部操作区
            Padding(
              padding: EdgeInsets.only(bottom: 24.h),
              child: Column(
                children: [
                  GestureDetector(
                    onTap: () {
                      BrnDialogManager.showConfirmDialog(context,
                          title: "确定退出登录",
                          cancel: '取消',
                          confirm: '确定',
                          message: "",
                          barrierDismissible: false, onConfirm: () {
                        Get.offAllNamed(Routes.LOGIN);
                        Api.loginOut();
                      }, onCancel: () {
                        Get.back();
                      });
                    },
                    child: Text('退出登录',
                        style: TextStyle(
                            color: const Color(0xFF607D8B), fontSize: 16.sp)),
                  ),
                  SizedBox(height: 8.h),
                  Text('版本号：v1.2.0',
                      style: TextStyle(color: Colors.grey, fontSize: 12.sp)),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildFuncCard({
    required IconData icon,
    required String label,
    required Color bgColor,
    required Color iconColor,
    required VoidCallback onTap,
  }) {
    return GestureDetector(
      onTap: onTap,
      child: AnimatedContainer(
        duration: const Duration(milliseconds: 120),
        curve: Curves.easeInOut,
        width: 80.w,
        height: 100.h,
        margin: EdgeInsets.only(bottom: 8.h),
        decoration: BoxDecoration(
          color: bgColor,
          borderRadius: BorderRadius.circular(16.r),
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.02),
              blurRadius: 2,
              offset: const Offset(0, 1),
            ),
          ],
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Icon(icon, color: iconColor, size: 28.w),
            SizedBox(height: 12.h),
            Text(label,
                style: TextStyle(fontSize: 15.sp, color: Colors.black87)),
          ],
        ),
      ),
    );
  }
}
